package cn.acewill.yxd.yunxundian.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.IOException;
import java.lang.ref.WeakReference;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.pubClass.AsynData;
import cn.acewill.yxd.yunxundian.pubClass.OkHttpManger;

/**
 * Created by XXX on 2017/6/8.
 */

public class SynchronizationAtivity extends Activity {

    //用于数据同步的activity

    private ProgressDialog dialog;
    private int userid;
    private int time=120;
    private Handler mHandler = new MyHandler(this);
    public static String[] taskType = {"getRowAddtionById","loadAllFeedBackData"};
    private int taskCount = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datasubmit);
        userid = getIntent().getIntExtra("userId", -1);
        dialog = new ProgressDialog(this);
        OkHttpManger.setDialog(dialog);
        if (!dialog.isShowing()) {
            dialog.show();
        }
        dialog.setMessage("正在同步信息");
        asyd.start();


    }
    private Thread asyd= new Thread() {
        @Override
        public void run() {
            AsynData data = new AsynData(mHandler);
            try {
                t.start();
                boolean flag = data.freshMyData(userid);
                if (flag) {
                    Message msg = new Message();
                    msg.what = 1;
                    msg.arg1 = taskType.length;
                    mHandler.sendMessage(msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    //计时
    private Thread t=new Thread(){
        @Override
        public void run() {
            while (time>=1){
                Message msg = new Message();
                msg.what = 0;
                msg.arg1 = 1;
                mHandler.sendMessage(msg);
                Log.e("aaaaaaaaaa",time+"s");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    private static class MyHandler extends  Handler{

        private WeakReference<SynchronizationAtivity> mActivity;
        MyHandler(SynchronizationAtivity activity){
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                mActivity.get().time= mActivity.get().time-msg.arg1;
                if (mActivity.get().time==2){
                    mActivity.get(). dialog.setMessage("自动跳转中");
                }
                else if (mActivity.get().time<=0){
                    mActivity.get().startActivity();
                }
            }else if(msg.what==1){
                if(msg.arg1<taskType.length)
                    Log.i("handleMessage","taskType = "+taskType[msg.arg1]);
                else
                    Log.i("handleMessage","taskType = freshMyData");
                mActivity.get().taskCount++;
                if(mActivity.get().taskCount>taskType.length)
                    mActivity.get().startActivity();
            }
        }
    }

    public void startActivity(){
        time=-1;
        startActivity(new Intent(this, MainActivity.class).putExtra("userId", userid));
        dialog.dismiss();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }
}
